(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('dataBasesonController', dataBasesonController);

function dataBasesonController(resourceServer) {
  var insertEcharts = echarts.init(document.getElementById('cpu1'));
  var equipmentEcharts = echarts.init(document.getElementById('cpu2'));
  var dataSpaceEcharts = echarts.init(document.getElementById('cpu3'));

  resourceServer.dataBaseDetail(function(res) {
    var data = spliceData(res.data.resources);
    var insertOptione = Echarts(data, '数据库资源使用情况', '时间(秒)', 'line');
    insertEcharts.setOption(insertOptione);
    var equipment = dataspliceData(res.data.equipmentSpace);
    var equipmentSpaceOption = oneEcharts(equipment.time, equipment.rate, '数据库设备使用率', '使用率(%)');
    var dataSpace = dataspliceData(res.data.dataSpace);
    var dataSpaceOption = oneEcharts(dataSpace.time, dataSpace.rate, '数据库空间使用率', '使用率(%)');
    equipmentEcharts.setOption(equipmentSpaceOption);
    dataSpaceEcharts.setOption(dataSpaceOption);

  });


  function spliceData(data) {
    var arr = [];
    arr.push(data.map(function(item) {
      return item.time
    }));
    arr.push(data.map(function(item) {
      return item.data.insertTime
    }));
    arr.push(data.map(function(item) {
      return item.data.selectTime
    }));
    arr.push(data.map(function(item) {
      return item.data.updateTime
    }));
    return arr;
  }


  function dataspliceData(data) {
    var arr = {};
    arr.time = data.map(function(item) {
      return item.time
    });
    arr.rate = data.map(function(item) {
      return item.rate
    });
    return arr;
  }


  function oneEcharts(xAxis, seriesdata, titleName, yname) {
    var option = {};
    option.tooltip = {
      trigger: 'axis'
    };
    option.title = {
      text: titleName
    };
    option.calculable = true;
    option.xAxis = [{
      type: 'category',
      boundaryGap: false,
      data: xAxis,
      axisLabel: {
        show: true
      }
    }];
    option.yAxis = [{
      type: 'value',
      name: yname
    }];
    option.series = [{
      type: 'line',
      stack: '总量',
      data: seriesdata
    }];
    return option;
  }


  function Echarts(data, titleName, xname, charType) {
    var option = {};
    option.series = [];
    option.title = {
      text: titleName
    };
    option.tooltip = {
      trigger: 'axis'
    };
    option.calculable = true;
    option.xAxis = [{
      name: '日期',
      type: 'category',
      boundaryGap: true,
      data: data[0]
    }];
    option.yAxis = [{
      type: 'value',
      name: xname
    }];
    for (var i = 1; i < data.length; i++) {
      option.series.push(chardate(data[i], charType))
    }
    return option;
  }

  function chardate(data, charType) {
    return {
      type: charType,
      data: data,
      itemStyle: {
        normal: {
          label: {
            show: true,
            position: 'top'
          }
        }
      },
      barMaxWidth: 13
    };
  }

}
dataBasesonController.$inject = ['resourceServer'];

})();